<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/10/28
 * Time: 13:38
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Zhengce extends Base
{
    /**
     * 首页政策解读展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            // 标题
            if(isset($params['zc_title']) && $params['zc_title'] != ''){
                $where['zc_title'] = ['like', '%' . $params['zc_title'] . '%'];
            }
            // 分类
            if(isset($params['zc_type']) && $params['zc_type'] != ''){
                $where['zc_type'] = $params['zc_type'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_zhengce')
                ->join('up_zhengce_type', 'zt_id = zc_type', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_zhengce')
                ->join('up_zhengce_type', 'zt_id = zc_type', 'left')
                ->field('zc_id, zc_title, zc_label, zc_image, zc_type, zc_num, zc_commnet_num, zc_time, zt_name')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('zc_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加政策解读
     * @return mixed|\think\response\Json
     */
    public function zhengceAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['zc_content'])){
                return json(['code' => 100, 'msg' => '请填写详情']);
            }
            $insert = [
                'zc_title' => $params['zc_title'],
                'zc_label' => $params['zc_label'],
                'zc_image' => $params['zc_image'],
                'zc_content' => $params['zc_content'],
                'zc_describe' => $params['zc_describe'],
                'zc_type' => $params['zc_type'],
                'zc_is_big' => $params['zc_is_big'],
                'zc_is_show_video' => $params['zc_is_show_video'],
                'zc_time' => date('Y-m-d H:i:s'),
                'zc_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_zhengce')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $type = Db::table('up_zhengce_type')->field('zt_id, zt_name')->select();
        $this->assign('type', $type);
        return $this->fetch('zhengce/zhengceadd');
    }
    
    /**
     * 编辑政策解读
     * @return mixed|\think\response\Json
     */
    public function zhengceEdit(){
        $zc_id = input('zc_id');
        if(Request::instance()->isPost()){
            $params = input('param.');
            if(empty($params['zc_content'])){
                return json(['code' => 100, 'msg' => '请填写详情']);
            }
            $update = [
                'zc_title' => $params['zc_title'],
                'zc_label' => $params['zc_label'],
                'zc_content' => $params['zc_content'],
                'zc_describe' => $params['zc_describe'],
                'zc_type' => $params['zc_type'],
                'zc_is_big' => $params['zc_is_big'],
                'zc_is_show_video' => $params['zc_is_show_video'],
                'zc_update_time' => date('Y-m-d H:i:s')
            ];
            if(!empty($params['zc_image'])){
                $update['zc_image'] = $params['zc_image'];
            }
            $res = Db::table('up_zhengce')->where('zc_id', $zc_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $zhengce = Db::table('up_zhengce')
            ->field('zc_title, zc_describe, zc_content, zc_label, zc_image, zc_type, zc_is_big, zc_is_show_video')
            ->where('zc_id', $zc_id)
            ->find();
        $type = Db::table('up_zhengce_type')->field('zt_id, zt_name')->select();
        $this->assign([
            'zc_id' => $zc_id,
            'zhengce' => $zhengce,
            'type' => $type
        ]);
        return $this->fetch('zhengce/zhengceedit');
    }
    
    /**
     * 政策解读详情
     * @return mixed
     */
    public function zhengceDetail(){
        $zc_id = input('zc_id');
        $zhengce = Db::table('up_zhengce')
            ->field('zc_title, zc_describe, zc_content, zc_label, zc_image, zc_type, zc_is_big, zc_is_show_video')
            ->where('zc_id', $zc_id)
            ->find();
        $type = Db::table('up_zhengce_type')->field('zt_id, zt_name')->select();
        $this->assign([
            'zhengce' => $zhengce,
            'type' => $type
        ]);
        return $this->fetch('zhengce/zhengcedetail');
    }
    
    /**
     * 删除政策解读
     * @return \think\response\Json
     */
    public function zhengceDel(){
        $zc_id = input('id');
        $res = Db::table('up_zhengce')->where('zc_id', $zc_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}